<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>04-练习作业-axios+swiper.html</title>
    <script src="/2025年6月/2025.6.17/axios.min.js"></script>
    <link rel="stylesheet" href="./swiper.min.css">
    <script src="./swiper.min.js"></script>

</head>

<body>
    <h1>通过axios请求数据接口获取图片-然后使用swiper实现轮播图效果</h1>
    <div class="swiper-container">
        <div class="swiper-wrapper" id="swiper-wrapper">
            <!-- 图片将通过JS动态添加到这里 -->
        </div>
        <!-- 如果需要分页器 -->
        <div class="swiper-pagination"></div>

        <!-- 如果需要导航按钮 -->
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
    </div>
    <script>
        /**
         * 接口地址: https://picsum.photos/v2/list
         * 请求方式: GET
         * 请求参数: limit: 数值, 需要的图片数量
         * 完整示例: https://picsum.photos/v2/list?limit=2
         * 
         * 
        */

        /*       
            
            数据接口响应数据结果如下所示:download_url属性为图片在线地址
                    
            [
                {
                "id": "0",
                "author": "Alejandro Escamilla",
                "width": 5000,
                "height": 3333,
                "url": "https://unsplash.com/photos/yC-Yzbqy7PY",
                "download_url": "https://picsum.photos/id/0/5000/3333"
                },
                {
                "id": "1",
                "author": "Alejandro Escamilla",
                "width": 5000,
                "height": 3333,
                "url": "https://unsplash.com/photos/LNRyGwIJr5c",
                "download_url": "https://picsum.photos/id/1/5000/3333"
                }
            ] 
        */
        axios({
            url: 'https://picsum.photos/v2/list',
            method: 'GET',
            params: {
                limit: 2
            }
        }).then(function (response) {
            // 成功处理
            console.log(11);
            const images = response.data;
            const swiperWrapper = document.getElementById('swiper-wrapper');
            // 遍历图片数据，创建Swiper幻灯片
            images.forEach(image => {
                const slide = document.createElement('div');
                slide.className = 'swiper-slide';
                slide.innerHTML = `<img src="${image.download_url}" alt="${image.author}">`;
                swiperWrapper.appendChild(slide);
            });

            // 初始化Swiper
            new Swiper('.swiper-container', {
                loop: true, // 循环模式
                autoplay: {
                    delay: 5000, // 自动切换时间间隔
                    disableOnInteraction: false // 用户操作后是否停止自动切换
                },
                // 如果需要分页器
                pagination: {
                    el: '.swiper-pagination',
                    clickable: true
                },
                // 如果需要前进后退按钮
                navigation: {
                    nextEl: '.swiper-button-next',
                    prevEl: '.swiper-button-prev',
                }
            });

        }).catch(function (error) {
            // 错误处理
            console.error('Error fetching images:', error);
        });



    </script>
</body>

</html>